import logging.config
import yaml
import logging
import os
import sys

if __name__ == '__main__':
    cur_dir = os.getcwd() + os.sep
    rootDir = cur_dir[:cur_dir.find("DownPython\\") + len("DownPython\\")]  # 获取myProject，也就是项目的根路径
    logging_config_file = rootDir + os.sep + 'common' + os.sep + 'logging_config.yaml'
    # 未使用
    # 设置日志
    with open(logging_config_file, 'r') as f:
        config = yaml.safe_load(f.read())
        logging.config.dictConfig(config)

    # logger = logging.getLogger('mylogger')
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    rf_handler = logging.StreamHandler(sys.stderr)  # 默认是sys.stderr
    rf_handler.setLevel(logging.DEBUG)
    # rf_handler = logging.handlers.TimedRotatingFileHandler('all.log', when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0))
    rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(message)s"))

    f_handler = logging.FileHandler('error.log.text')
    f_handler.setLevel(logging.ERROR)
    f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))

    logger.addHandler(rf_handler)
    logger.addHandler(f_handler)

    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warning message')
    logger.error('error message')
    logger.critical('critical message')
